package cn.youyeku.zsxh.ui.fragment.ABS;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.R;

import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.adapter.RefreshFootAdapter;
import cn.youyeku.zsxh.api.ResponseStatus;
import cn.youyeku.zsxh.app;


public abstract class AbsSwipeFragment<T> extends Fragment implements SwipeRefreshLayout.OnRefreshListener, ResponseStatus {


    RecyclerView mRcyView;
    protected List<T> mData = new ArrayList<>();
    RefreshFootAdapter<T> mRefreshAdapter;
    LinearLayoutManager layoutManager;
    SwipeRefreshLayout mSwipeRefreshLayout;

    protected int mRcyLayoutView;
    protected int mRcyId;
    protected int mRcySwipId;
    protected int mRcyItemR;
    protected int mRcyFooterR = R.layout.refresh_footer; //default
    protected View view;

    private int cfgPreLoadingNum = 0; //

    protected abstract RefreshFootAdapter<T> getAapter();


    protected abstract void requestData();

    protected Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case STATUS_SUCCESS:

                    mRefreshAdapter.addMoreItem(mData);

                    mRefreshAdapter.changeStatus(mRefreshAdapter.STATUS_UP_LOAD_MORE);
                    break;
                case STATUS_NOT_MORE_DATA:
                    mRefreshAdapter.changeStatus(mRefreshAdapter.STATUS_NOT_MORE_DATA);
                    break;
                case STATUS_NO_LOGIN:
                    app.backgroundThreadShortToast("你还没有登录");
                    UIControl.showLogin(getActivity());
                    break;
                case STATUS_GUOQI:
                    app.backgroundThreadShortToast("你还没有登录");
                    UIControl.showLogin(getActivity());
                    break;
                case STATUS_UNACTIVE:
                    app.backgroundThreadShortToast("你已经登录但是还没激活帐号");
                    UIControl.showUnActiveActivity(getContext());
                    break;


            }

            mSwipeRefreshLayout.setRefreshing(false);
        }
    };


    /**
     * @param mRcyLayoutView 包含recycleview的布局
     * @param mRcyId         recycle的id
     * @param mRcySwipId     下拉刷新的id
     */
    public AbsSwipeFragment(int mRcyLayoutView, int mRcyId, int mRcySwipId) {

        this.mRcyLayoutView = mRcyLayoutView;
        this.mRcyId = mRcyId;
        this.mRcySwipId = mRcySwipId;


    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        view = inflater.inflate(mRcyLayoutView, container, false);


        mRcyView = (RecyclerView) view.findViewById(mRcyId);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(mRcySwipId);


        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        mSwipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));

        mSwipeRefreshLayout.setOnRefreshListener(this);
        //setting layout manage
        setLayoutV_H(LinearLayoutManager.VERTICAL);

        mRefreshAdapter = getAapter();

        onRefresh();


        mRcyView.setAdapter(mRefreshAdapter);


        mRcyView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int totalItemCount = layoutManager.getItemCount();
                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItem >= totalItemCount - cfgPreLoadingNum && lastVisibleItem < 20) {
                    LogUtils.w(lastVisibleItem + "前面可视化item   全部的item " + totalItemCount + " 预读" + cfgPreLoadingNum);

                    onRefresh();

                }
            }
        });

        initView();

        return view;


    }

    protected abstract void initView();


    /**
     * @param type 设置水平方向还是垂直布局
     */
    public void setLayoutV_H(int type) {
        layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(type);
        mRcyView.setLayoutManager(layoutManager);
    }

    @Override
    public void onRefresh() {
        mRefreshAdapter.changeStatus(mRefreshAdapter.STATUS_LOADING);

        requestData();


    }




}
